package io.github.maojian.str;

import org.junit.Test;

import java.util.Arrays;

/**
 * @Auther: jian.mao
 * @Date: 2022/4/19 12:55
 * @Description:
 */
public class Q821 {
    public int[] shortestToChar(String s, char c) {
        int[] result = new int[s.length()];
        int curVal = s.length() - 1;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                curVal = i;
            }
            result[i] = Math.abs(i - curVal);
        }
        curVal = 0;
        for (int i = s.length() - 1; i >= 0; i--) {
            if (s.charAt(i) == c) {
                curVal = i;
            }
            result[i] = Math.min(result[i], Math.abs(i - curVal));
        }
        return result;
    }

    @Test
    public void test() {
        String s = "loveleetcode";
        char c = 'e';
        System.out.println(Arrays.toString(shortestToChar(s, c)));

        s = "aaab";
        c='b';
        System.out.println(Arrays.toString(shortestToChar(s, c)));

    }
}
